package demo2接口使用案例;

import java.util.Arrays;

class Student implements Comparable<Student>{//这种方式堆类的侵入性比较强，所以做好不要去改动原有的逻辑，比如我按名字排序这个时候就会发生变化
    public String name ;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age - o.age;//大于0，则前者大于后者，则排序的顺序不动，小于0，前者小于后者，则排序的顺序会改变
        /*if(this.age > o.age){
            return 1;
        }else if(this.age < o.age){
            return -1;
        }else{
            return 0;
        }//自己手动实现
        */
    }
}
public class Test {
    public static void bubbleSort(Comparable[] comparable){
        for (int i = 0; i < comparable.length; i++) {
            for (int j = 0; j < comparable.length-i-1; j++) {
                if(/*comparable[j] > comparable[j+1]引用类型不能直接下载*/comparable[j].compareTo(comparable[j+1]) > 0){
                    Comparable tmp = comparable[j];
                    comparable[j] = comparable[j+1];
                    comparable[j+1] = tmp;
                }
            }
        }
    }
    public static void main(String[] args) {
        /*Student student1 = new Student("zhangsan", 10);
        Student student2 = new Student("lisi", 15);
        //比较连个学生是否一样大小
//        System.out.println(student1 > student2);//这种是类类型（引用类型），无法进行直接比较
        System.out.println(student1.compareTo(student2));

        //如果是自定义类型，要想比较大小，就要实现Comparable这个接口，从而实现了这个比较的逻辑

        System.out.println("=================");
*/
        //利用对象数组
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",10);
        students[1] = new Student("lisi",2);
        students[2] = new Student("niulianqiang",18);

        bubbleSort(students);//自己手动实现

       /* Arrays.sort(students);//这个时候要用comparable来进行一定的顺序排序，
        //因为通过comparable来进行查找一定的内容，从而按一定的内容来进行排序了*/
        System.out.println(Arrays.toString(students));
    }
}
